package kpython.backend.ir.canon;

import kpython.backend.ir.tree.IRTreeVisitor;
import kpython.backend.ir.tree.Stmt;

public class AccessFunction extends Stmt {

	public StringBuilder code = new StringBuilder();
	
	public AccessFunction() {
		code.append("goto access_skip;\n");
		code.append("access:\n");		
		
		code.append("if( _rv->type == v_integer)\n");
		code.append("\tsp->locals[0].value.integer = _rv->value.integer; \n");
		code.append("if( _rv->type == v_string)\n");
		code.append("\tsp->locals[0].value.string = _rv->value.string; \n");
		code.append("if( _rv->type == v_float)\n");
		code.append("\tsp->locals[0].value.real = _rv->value.real;\n");
		code.append("if( _rv->type == v_boolean)\n");
		code.append("\tsp->locals[0].value.boolean = _rv->value.boolean; \n");
		code.append("sp->locals[0].type = _rv->type;\n");
				
		code.append("goto redirector;\n");
		code.append("access_skip:\n");
	}
	
	@Override
	public Object accept(IRTreeVisitor visitor, Object data) {		
		return visitor.visit(this, data);
	}

	@Override
	public void printTree(String tab) {		
		System.out.println(tab+this.getClass().getSimpleName());
		tab = tab+"\t";
		System.out.println(tab+"Label access");		
	}
	
	public String toString() {
		return this.getClass().getSimpleName();
	}

}
